<template>
  <div class="login-wrap">
    <el-card class="container">
      <div slot="header" class="login-title">博客后台管理系统</div>
      <el-form ref="loginForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名:" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="密 码:" prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            clearable
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    login() {
      {
        // 校验表单
        // 通过this.$refs['loginForm']  得到form组件对象
        // console.log(this.$refs['loginForm'])
        this.$refs['loginForm'].validate((valid) => {
          // valid为true:表单校验通过
          if (valid) {
            // 发送post请求,比对用户名和密码

            // 提示登陆成功
            this.$message({
              message: '登陆成功',
              type: 'success',
              showClose: true,
              duration: 1000,
            })
            // this.$message.success('登陆成功')
            // 跳转到后台的首页
            this.$router.push('/')
          }
        })
      }
    },
  },
}
</script>

<style>
.login-wrap {
  display: flex;
  height: 100vh;
  background-color: #294669;
  background-image: url('../assets/images/bg1.jpg');
  align-items: center;
  justify-content: center;
}
.container {
  width: 400px;
  height: 270px;
  background-color: rgba(255, 255, 255);
}
.login-title {
  text-align: center;
}
</style>
